// 在C++98中一般只支持用{}来初始化结构体和数组,但在C++11中尝试支持万事万物都可以用{}来初始化,{}也叫列表初始化

#include <iostream>
#include<vector>
using namespace std;

struct Point {
    int _x;
    int _y;
};

class Date {
public:
    Date(int year = 1, int month = 1, int day = 1)
        : _year(year), _month(month), _day(day) {
        std::cout << "Date(int year, int month, int day)" << std::endl;
    }
    Date(const Date& d)
        : _year(d._year), _month(d._month), _day(d._day) {
        std::cout << "Date(const Date& d)" << std::endl;
    }
    void Print()
    {
      std::cout << _year << _month << _day << std::endl;
    }
private:
    int _year;
    int _month;
    int _day;
};

void Push_Back(Date t)
{
    ;
}
int main() {
    // c++98
    //int a1[] = { 1, 2, 3, 4 };
    //int a2[5] = { 0 };
    //Point p = { 1, 2 };
    // c++11
    // 内置类型使用{}初始化
    //int x1 = { 2 };


    //都是通过构造函数先构造一个匿名对象,然后调用拷贝构造,编译器优化为直接构造
    //Date d0 = 2027;
    //std::cout << "-------------------------------------------"<< std::endl;

    //Date d1 = {2025 , 1 , 1};
    //std::cout << "-------------------------------------------"<< std::endl;

    //Date d2 = Date(2024);
    //std::cout << "-------------------------------------------"<< std::endl;

    //临时对象具有常性所以要加const
    //const Date& d3 = 2020;
    //const Date& d4 = {2029};

    //列表初始化的应用场景
    vector<Date> v(3);

    //会与扩容有关系导致多次拷贝(异地扩容)
    std::cout << "-------------------------------------------" << std::endl;
    //以前写法
    Date d5 = 2009;
   // v.push_back(d5);
    v[0] = d5;

    std::cout << "-------------------------------------------" << std::endl;
    //或
    // v.push_back(Date(1111));
    v[1] = Date(1111);

    std::cout << "-------------------------------------------" << std::endl;

    //现在写法
    // v.push_back({ 2009 });
    v[2] = { 2009 };

    std::cout << "-------------------------------------------" << std::endl;

    // Date d5 = 2009;
    // Push_Back(d5);

    // Push_Back(Date(1111));

    // Push_Back({ 2001 });
    for(int i = 0; i < 3; i++)
    {
      v[i].Print();
    }

    //通过{}初始化也可以省略=

    Point p1{1,2};

    int x {2};
    printf("%d\n",2);

    return 0;
}